﻿using System;
using WeDonekRpc.Helper;
using WeDonekRpc.PipeServer.Client;
using WeDonekRpc.PipeServer.Config;
using WeDonekRpc.PipeServer.Model;
namespace WeDonekRpc.PipeServer.Log
{
    internal class IoLogSystem
    {
        private static readonly string _LogGroup = "PipeServerLog";
        private static readonly string _SendGroup = "SendException";

        private static readonly string _ReceiveGroup = "ReceiveException";


        private static readonly string _InitGroup = "InitException";

        private static readonly string _AcceptGroup = "AcceptException";

        private static readonly string _ReplyPageError = "ReplyPageError";

        public static void AddSendLog (Exception e, PipeClient client)
        {
            if (PipeLogConfig.CheckIsRecord(_SendGroup, out LogGrade grade))
            {
                ErrorException error = ErrorException.FormatError(e, grade);
                error.Args.Add("PipeName", client.PipeName);
                error.Save(_LogGroup);
            }
        }
        public static void AddReceiveLog (Exception e, PipeClient client)
        {
            if (PipeLogConfig.CheckIsRecord(_ReceiveGroup, out LogGrade grade))
            {
                ErrorException error = ErrorException.FormatError(e, grade);
                error.Args.Add("PipeName", client.PipeName);
                error.Args.Add("ClientName", client.ClientName);
                error.Save(_LogGroup);
            }
        }

        internal static void AddInitLog (Exception e, PipeClient client)
        {
            if (PipeLogConfig.CheckIsRecord(_InitGroup, out LogGrade grade))
            {
                ErrorException error = ErrorException.FormatError(e, grade);
                error.Args.Add("PipeName", client.PipeName);
                error.Args.Add("ClientName", client.ClientName);
                error.Save(_LogGroup);
            }
        }


        internal static void AddAcceptLog (Exception e, PipeClient client)
        {
            if (PipeLogConfig.CheckIsRecord(_AcceptGroup, out LogGrade grade))
            {
                ErrorException error = ErrorException.FormatError(e, grade);
                error.Args.Add("PipeName", client.PipeName);
                error.Save(_LogGroup);
            }
        }

        internal static void AddReplyError (ReplyPage page, string error)
        {
            if (PipeLogConfig.CheckIsRecord(_ReplyPageError, out LogGrade grade))
            {
                LogInfo log = new LogInfo(error, grade, _LogGroup)
                {
                    { "PageId",page.Page.PageId.ToString()},
                    { "Type",page.Page.Type},
                    {"BindParam",page.Client.BindParam },
                    { "PipeName",page.Client.PipeName}
                };
                log.LogTitle = "数据包回复失败!";
                log.Save();
            }
        }
    }
}
